package com.wteam.Service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.PoJo.DO.user;
import com.wteam.PoJo.DTO.UserLoginDTO;
import com.wteam.PoJo.DTO.reflushloginDTO;
import com.wteam.PoJo.Properties.WeChatproperties;
import com.wteam.PoJo.VO.UserLoginVO;
import com.wteam.Service.WeChatService;
import com.wteam.framework.common.security.enums.UserEnums;
import com.wteam.framework.common.security.token.Token;
import com.wteam.framework.common.security.token.myTokenUtil;
import com.wteam.framework.common.utils.HttpClientUtils;
//import com.wteam.Mapper.WeChatmapper;
import com.wteam.mapper.WeChatMapper;
import com.wteam.until.Result;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service

public class WeChatServiceimpl extends ServiceImpl<WeChatMapper, user> implements WeChatService {
    @Autowired
    private myTokenUtil mytokenUtil;
    @Autowired
    private WeChatMapper weChatMapper;
    @Autowired//微信登录appid信息
    private WeChatproperties weChatproperties;
    //微信服务接口地址
    private static final String WE_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result login(UserLoginDTO userLoginDTO) {
        //调用微信接口文档
        Map<String, String> map = new HashMap();

        map.put("appid", weChatproperties.getAppid());
        map.put("secret", weChatproperties.getSecret());
        map.put("js_code", userLoginDTO.getCode());
        map.put("grant_type", "authorization_code");
        log.info(map.toString());
        String json = HttpClientUtils.doGet(WE_LOGIN, map);
        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");
        if (openid == null) {
            throw new RuntimeException("系统异常");
        }
        log.info(map.toString());
        //查看数据库是否有这个用户，没有注册
        LambdaQueryWrapper<user> loginUser = new LambdaQueryWrapper<>();
        loginUser.eq(user::getOpenid, openid);
        user user = weChatMapper.selectOne(loginUser);
        if (Objects.isNull(user)) {
            //注册账号
            user NewUser = new user();
            NewUser.setOpenid(openid);
            NewUser.setCreateTime(LocalDateTime.now());
            weChatMapper.insert(NewUser);
        }


        com.wteam.PoJo.DO.user nowUser = lambdaQuery().eq(com.wteam.PoJo.DO.user::getOpenid, openid).one();

        //生成jwt
        Map<String, Object> claams = new HashMap<>();
        claams.put("id", nowUser.getId());
        Token token = mytokenUtil.createToken(claams, true, UserEnums.MEMBER);
//        String s = jwt.generateJwt(claams);

        //封装vo返回
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setOpenid(openid);
        userLoginVO.setId(nowUser.getId());
        userLoginVO.setToken(token);
        System.out.println(userLoginVO);
//        //存redis
//        String key = "login_" + nowUser.getId();
//        redisTemplate.opsForValue().set(key, loginUser);
        return Result.success(userLoginVO);

    }

    @Override
    public Result<Token> reflushlogin(reflushloginDTO reflushloginDTO) {
        Token token = mytokenUtil.refreshToken(reflushloginDTO.getRefreshToken(), UserEnums.MEMBER);
        return Result.success(token);
    }
}
